package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.jmp.base.UserApi;
import com.jmp.base.dto.*;
import lombok.extern.slf4j.Slf4j;
import one.stand.advice.ResultEnum;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.AdminMapperExt;
import one.stand.model.Admin;
import one.stand.model.ResultModel;
import one.stand.model.User;
import one.stand.model.UserView;
import one.stand.service.ThumbsService;
import one.stand.service.UserService;
import one.stand.service.UserViewService;
import one.stand.util.AssertUtil;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@Slf4j
public class UserController implements UserApi {

    @Autowired
    private ThumbsService thumbsService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserViewService userViewService;

    @Autowired
    private AdminMapperExt adminMapperExt;

    @Override
    public ResultModel thumbs(@RequestBody UserThumbsDto dto) {

        thumbsService.save(dto.getUserId(), dto.getItemType(), dto.getItemId());

        return ResultModel.success();
    }

    @Override
    public ResultModel viewSave(@RequestBody UserViewSaveDto dto) {

        userViewService.save(dto.getUserId(), dto.getType());

        return ResultModel.success();
    }

    @Override
    public ResultModel<UserViewVo> view(@RequestBody UserViewDto dto) {
        Map<Integer, Date> viewMap = Maps.newHashMap();
        for (Integer type : dto.getTypes()) {
            UserView userView = userViewService.get(dto.getUserId(), type);
            viewMap.put(type, Objects.nonNull(userView) ? userView.getEndTime() : UserViewService.DEFAULT_END_TIME);
        }
        UserViewVo vo = UserViewVo.builder()
                .viewMap(viewMap)
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel updateImgLocal() {
        userService.updateImgToLocal();

        return ResultModel.success();
    }

    @Override
    public ResultModel<UserAdminListVo> adminList(@RequestBody UserAdminListDto dto) {
        String nameLike = SqlUtil.like(dto.getKeyword());
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<Admin> list = adminMapperExt.selectList(nameLike, null);
        PageResponse2<UserAdminListVo.Admin> pageResponse2 = ConvertUtils.page2(list, admin -> {
            return UserAdminListVo.Admin.builder()
                    .adminId(admin.getAdminId())
                    .loginName(admin.getLoginName())
                    .name(admin.getName())
                    .sex(admin.getSex())
                    .position(admin.getPosition())
                    .tourist(admin.getTourist())
                    .createTime(admin.getCreateTime())
                    .isDel(admin.getIsDel())
                    .build();
        });

        UserAdminListVo vo = UserAdminListVo.builder()
                .page(pageResponse2)
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel<UserAdminSaveVo> adminSave(@RequestBody UserAdminSaveDto request) {
        Admin admin = new Admin();
        admin.setAdminId(request.getAdminId());
        admin.setLoginName(request.getLoginName());
        admin.setPassWord(request.getPassWord());
        admin.setName(request.getName());
        admin.setSex(request.getSex());
        admin.setPosition(request.getPosition());
        admin.setIsDel(request.getIsDel());
        Integer id = userService.saveAdmin(admin);

        UserAdminSaveVo vo = UserAdminSaveVo.builder().id(id).build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel save(@RequestBody UserSaveDto request) {

        if (Objects.nonNull(request.getId())) {
            User user = new User();
            user.setUserId(request.getId());
            user.setAvatarUrl(request.getAvatarUrl());
            user.setNickeName(request.getNickeName());
            userService.update(user);
        } else {
            AssertUtil.fail(ResultEnum.FAIL);
        }

        return ResultModel.success();
    }

    @Override
    public ResultModel merge(@RequestBody UserMergeDto request) {
        userService.merge(request.getMergeUserId(), request.getUserId());
        return ResultModel.success();
    }
}
